﻿//#include<iostream>
//using namespace std;
//
//class a
//{
//public:
//	a(int a = 0)
//		: _a(a)
//	{
//		cout << "a():" << this << endl;
//	}
//	~a()
//	{
//		cout << "~a():" << this << endl;
//	}
//private:
//	int _a;
//};
//int main()
//{
//	
//	a* p1 = (a*)malloc(sizeof(a)*3);
//	a* p2 = new a(3);
//	free(p1);
//	delete  p2;
//	
//	return 0;
//}




#include<iostream>
using namespace std;
typedef int STDataType;
class Stack
{
public:
	Stack(int n = 4)
	{
		_a = new STDataType [n];
		
		_capacity = n;
		_top = 0;
	}
	Stack(const Stack& st)
	{
		_a = new STDataType[st._capacity];
		if (nullptr == _a)
		{
			perror("malloc申请空间失败!!!");
			return;
		}
		memcpy(_a, st._a, sizeof(STDataType) * st._top);
		_top = st._top;
		_capacity = st._capacity;
	}
	void Push(STDataType x)
	{
		if (_top == _capacity)
		{
			int newcapacity = _capacity * 2;
			STDataType* tmp = (STDataType*)realloc(_a, newcapacity *
				sizeof(STDataType));
			if (tmp == NULL)
			{
				perror("realloc fail");
				return;
			}
			_a = tmp;
			_capacity = newcapacity;
		}
		_a[_top++] = x;
	}
	~Stack()
	{
		cout << "~Stack()" << endl;
		free(_a);
		_a = nullptr;
		_top = _capacity = 0;
	}
private:
	STDataType* _a;
	size_t _capacity;
	size_t _top;
};
int main()
{
	//假设要在堆区上申请一个栈对象
	Stack* st = new Stack(10);

	delete st;

	return 0;
}
